﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;

namespace FoundationLib.Common
{
	/// <summary>
	/// Provides options for data paging.
	/// </summary>
	public class PagingOptions
	{
		/// <summary>
		/// The page index
		/// </summary>
		private int index;
		/// <summary>
		/// The maximum number of records per page
		/// </summary>
		private int size;


		/// <summary>
		/// Creates new <see cref="PagingOptions"/>.
		/// </summary>
		/// <param name="index">The 1-based page index</param>
		/// <param name="size">The maximum number of records per page</param>
		public PagingOptions(int index, int size)
		{
			Contract.Requires(index > 0 && size > 0);
			Contract.Ensures(this.index == index && this.size == size);

			this.index = index;
			this.size = size;
		}

		/// <summary>
		/// Gets or sets the 1-based page index.
		/// </summary>
		/// <remarks>
		/// The first page has index 1.
		/// </remarks>
		public int Index
		{
			get
			{
				return this.index;
			}
			set
			{
				Contract.Requires(value > 0);
				this.index = value;
			}
		}

		/// <summary>
		/// Gets or sets the maximum number of records per page.
		/// </summary>
		public int Size
		{
			get
			{
				return this.size;
			}
			set
			{
				Contract.Requires(value > 0);
				this.size = value;
			}
		}

		[ContractInvariantMethod]
		private void ObjectInvariant()
		{
			Contract.Invariant(this.index > 0);
			Contract.Invariant(this.size > 0);
		}

		/// <summary>
		/// Gets the record offset for the current paging options.
		/// </summary>
		public int Offset
		{
			get
			{
				return ((this.index - 1) * this.size);
			}
		}
	}
}
